﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.cruxomatic.BusinessObjects;
using com.cruxomatic.controllers.Administration;
using com.cruxomatic.DTOs;
using System.Collections.Generic;
using com.cruxomatic.views.Administration;
using com.cruxomatic.views.Enums;
using com.cruxomatic.DTOs.Configuration;
using com.cruxomatic.configuration;

public partial class Administration_ManageCompanies : BaseView, IManageCompanies
{
    ManageCompaniesController _controller;

    protected void Page_Load(object sender, EventArgs e)
    {
        int startingRowCount = Convert.ToInt32(CruxConfigurationManager.GetApplicationCustomSetting("PagerSize").Value);
        TableSortSearchPagination1.NumberOfRowsCsv = string.Format("{0},{1},{2},{3}", startingRowCount,
                                                                   startingRowCount * 2, startingRowCount * 4, startingRowCount * 6);
        _controller = new ManageCompaniesController(this);
        _controller.InitView(IsPostBack);
        if (!IsPostBack)
        {
            Page.DataBind();
            #region Site Map
            UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.ManageOrganization);
            #endregion
        }
    }

    public void grdCompanies_RowCommand(object sender, CommandEventArgs e)
    {
        string companyId = e.CommandArgument.ToString();
        RowCommands rowCommand = (RowCommands)Enum.Parse(typeof(RowCommands), e.CommandName);
        if (rowCommand == RowCommands.EditItem)
        {
            _controller.OpenCompany(int.Parse(companyId));
            UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.CreateLeafUrlTreeNode("Edit - " + CurrentCompany.CompanyName, UrlTreeNodeItems.ManageOrganization));
        }
        else if (rowCommand == RowCommands.DeleteItem)
            _controller.DeleteCompany(int.Parse(companyId));
        else if (rowCommand == RowCommands.OpenView)
            _controller.OpenCompanyAdministrationView(int.Parse(companyId));
    }

    protected void grdCompanies_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton deleteButton = (ImageButton)e.Row.FindControl("btnItemDelete");
            string ReplaceConfirmation = new UILiterals().DeleteCompanyConfirmation.Replace("{0}",
                DataBinder.Eval(e.Row.DataItem, "CompanyName").ToString());
            ReplaceConfirmation = LocalizationManager.GetLocalizedText(ReplaceConfirmation);
            deleteButton.Attributes.Add("onclick", "javascript:return "
                + "confirm('" + ReplaceConfirmation + "')");

            Panel pnlAddEditCompany = (Panel)e.Row.FindControl("pnlEditDeleteCompany");
            if (IsEditCompanySupported) pnlAddEditCompany.Visible = true;
            else pnlAddEditCompany.Visible = false;
        }
    }


    #region IManageLocations Members

    public List<CompanyInformation> Companies
    {
        get
        {
            return (List<CompanyInformation>)grdCompanies.DataSource;
        }
        set
        {
            grdCompanies.DataSource = value;
            grdCompanies.DataBind();
        }
    }

    public CompanyInformation CurrentCompany
    {
        get
        {
            CompanyInformation companyInformation = new CompanyInformation();
            companyInformation.CompanyID = int.Parse(lblCompanyIDValue.Text);
            companyInformation.CompanyName = txtCompanyName.Text;
            companyInformation.CompanyTypeID = int.Parse(drpCompanyType.SelectedValue);
            companyInformation.CompanyType = drpCompanyType.SelectedItem.Text;

            companyInformation.CompanyBillingAddress = txtCompanyBillingAddress.Text;
            companyInformation.CompanyMailingAddress = txtCompanyMailingAddress.Text;
            companyInformation.CompanyComments = txtCompanyComments.Text;
            List<CompanyMasterLocationInformation> companylocations = new List<CompanyMasterLocationInformation>();
            foreach (var item in SelectedMasterLocations)
            {
                companylocations.Add(new CompanyMasterLocationInformation() {CompanyID = companyInformation.CompanyID, MasterLocationID = item.MasterLocationID});
            }
            companyInformation.MasterLocations = companylocations;

            return companyInformation;
        }
        set
        {
            lblCompanyIDValue.Text = value.CompanyID.ToString();
            txtCompanyName.Text = value.CompanyName;
            txtCompanyBillingAddress.Text = value.CompanyBillingAddress;
            txtCompanyMailingAddress.Text = value.CompanyMailingAddress;
            txtCompanyComments.Text = value.CompanyComments;
            if (value.CompanyTypeID != 0)
                drpCompanyType.SelectedValue = value.CompanyTypeID.ToString();
        }
    }

    public FormMode FormMode
    {
        get
        {
            if (pnlList.Visible)
                return FormMode.List;
            if (pnlAddEdit.Visible)
                return FormMode.AddEdit;
            throw UIExceptions.InvalidFormModeException;
        }
        set
        {
            if (value == FormMode.List)
            {
                pnlList.Visible = true;
                pnlAddEdit.Visible = false;
            }
            else if (value == FormMode.AddEdit)
            {
                pnlList.Visible = false;
                pnlAddEdit.Visible = true;
            }
            else
            {
                throw UIExceptions.InvalidFormModeException;
            }
        }
    }

    #endregion
    protected void btnSave_Click(object sender, EventArgs e)
    {
        _controller.SaveCompany();
        #region Site Map
        if (FormMode == FormMode.List)
            UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.ManageOrganization);
        #endregion
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        _controller.CancelAddEdit();
        #region Site Map
        UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.ManageOrganization);
        #endregion
    }
    protected void btnAddNewCompany_Click(object sender, EventArgs e)
    {
        #region Site Map
        UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.CreateLeafUrlTreeNode("Add", UrlTreeNodeItems.ManageOrganization));
        #endregion
        _controller.OpenCompany(0);
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        _controller.RedirectToPreviousPage();
    }

    #region IManageLocations Members


    public ErrorInformation ErrorDetails
    {
        get
        {
            return pnlError.ErrorInformation;
        }
        set
        {
            pnlError.ErrorInformation = value;
        }
    }

    public bool IsAddCompanySupported
    {
        get { return btnAddNewCompany.Visible; }
        set { btnAddNewCompany.Visible = value; }
    }

    public Administration_ManageCompanies()
    {
        IsEditCompanySupported = false;
    }

    public bool IsEditCompanySupported { get; set; }

    #endregion



    #region IManageCompanies Members


    public List<CompanyTypeInformation> AllowedCompanyTypes
    {
        set { SetCompanyType(value); }
    }

    private void SetCompanyType(List<CompanyTypeInformation> AllowedCompanyTypes)
    {
        drpCompanyType.Items.Clear();
        foreach (CompanyTypeInformation item in AllowedCompanyTypes)
        {
            ListItem listItem = new ListItem();
            listItem.Text = item.CompanyTypeName;
            listItem.Value = item.CompanyTypeID.ToString();
            drpCompanyType.Items.Add(listItem);
        }
    }

    #endregion


    public bool IsEditCompanyNameSupported
    {
        set
        {
            txtCompanyName.ReadOnly = !value;
        }
    }

    public bool IsMasterLocationsSupported
    {
        set
        {
            trMasterLocations.Visible = value;
        }
    }

    public List<MasterLocationInformation> AllMasterLocations
    {
        set
        {
            ddlAllMasterLocations.DataSource = value;
            ddlAllMasterLocations.DataTextField = "LocationName";
            ddlAllMasterLocations.DataValueField = "MasterLocationID";
            ddlAllMasterLocations.DataBind();
        }

    }
    public List<MasterLocationInformation> SelectedMasterLocations
    {
        get
        {
            List<MasterLocationInformation> locations = new List<MasterLocationInformation>();
            foreach (ListItem item in ddlSelectedMasterLocations.Items)
            {
                MasterLocationInformation location = new MasterLocationInformation();
                location.MasterLocationID = int.Parse(item.Value);
                location.LocationName = item.Text;
                locations.Add(location);
            }
            return locations;
        }

        set
        {
            ddlSelectedMasterLocations.DataSource = value;
            ddlSelectedMasterLocations.DataTextField = "LocationName";
            ddlSelectedMasterLocations.DataValueField = "MasterLocationID";
            ddlSelectedMasterLocations.DataBind(); 
        }}

    protected void btnSelectLocation_OnClick(object Sender, EventArgs E)
    {
        List<ListItem> itemstoAdd = new List<ListItem>();
        foreach (ListItem item in ddlAllMasterLocations.Items)
        {
            if (item.Selected)
            {
                itemstoAdd.Add(item);
            }
        }
        foreach (ListItem item in itemstoAdd)
        {
            ddlSelectedMasterLocations.Items.Add(item);
            ddlAllMasterLocations.Items.Remove(item);
        }
        ddlSelectedMasterLocations.SelectedIndex = 0;
        
    }

    protected void btnRemoveLocation_OnClick(object Sender, EventArgs E)
    {
        List<ListItem> itemstoAdd = new List<ListItem>();
        foreach (ListItem item in ddlSelectedMasterLocations.Items)
        {
            if (item.Selected)
            {
                itemstoAdd.Add(item);
            }
        }
        foreach (ListItem item in itemstoAdd)
        {
            ddlAllMasterLocations.Items.Add(item);
            ddlSelectedMasterLocations.Items.Remove(item);
        }
        ddlAllMasterLocations.SelectedIndex = 0;
     }
}

